﻿using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.Events;

/// <summary>
/// Module管理器, 需要在每个module的第一个场景的入口(eg: start)处调用一次<see cref="ModuleManager"/>的<see cref="EnterModule"/>方法;
/// 调用一次<see cref="ModuleManager"/>的<see cref="EnterModule"/>方法, 会干两件事:
/// 1. <see cref="ModuleManager"/>会以Module Name为参数，向服务器发送一次ScreenEvent，项目程序员也不需要自己再发一次了.
/// </summary>
public class ModuleManager : MonoBehaviour {

	private static ModuleManager m_instance;
	/// <summary>
	/// <see cref="ModuleManager"/>单例.
	/// </summary>
	public static ModuleManager Instance{
		get{
			if(m_instance==null){
				GameObject go = new GameObject("[ModuleManager]");
				return go.AddComponent<ModuleManager>();
			}
			return m_instance;
		}
	}


	//当前module name..如果EnterModule和当前
	private string _currentModuleName = "";

	void Awake(){
		if (m_instance){
			Destroy(this.gameObject);
			return;
		}
		m_instance = this;
		DontDestroyOnLoad(gameObject);
	}

	/// <summary>
	/// 进入(or切换)Module，需要在module第一个场景的入口处调用.
	/// </summary>
	/// <param name="moduleName">Module名字(按理应该由PM提供).</param>
	/// <param name="isShowAd">是否显示cbcp或全屏广告.</param>
	public void EnterModule(string moduleName,bool isShowAd=false)
	{
		if (_currentModuleName.Equals (moduleName))
			return;
		_currentModuleName = moduleName;
		//send screen event
		PluginManager.Instance.analyticsPlugin.SendScreenEvent (moduleName);
		//cross logic
		if(isShowAd && SceneManager.GetActiveScene ().buildIndex > PluginManager.Instance.showBannerSceneIdx-1 && !PluginManager.Instance.IsAdRemovedByIAP) {
			PluginManager.Instance.adsPlugin.ShowAd((int)AdType.AdTypeInterstitialAds);
		}
	}
}
